/*
* ============================================================================
* 功能描述 ：应用层配置
* 作者       ：ggls
* 创建日期 ：2024.04.10
* ----------------------------------------------------------------------------
* 更新历史 ：
*	日期 　　- 修改者   : 更新内容描述
*
* ============================================================================
*/

#include "main.h"
#include <stdio.h>


static app_data_s app_data;


#if 0
static void _temp_handle(signed short t, signed short h)
{
	unsigned char t10 = 0, t100 = 0, t1000 = 0;
	unsigned char h10 = 0, h100 = 0, h1000 = 0;
	
	t10 	= (t/10%10);		// 十位位
	t100 	= (t/100%10);		// 百位
	t1000 	= (t/1000);			// 千位
	DelayXms(2);
//	t_10 	= _temp_lcd(t10);
//	t_100 	= _temp_lcd(t100);
//	t_1000 	= _temp_lcd(t1000);

	j1  = SHORT_GET_HIGH_BYTE(h);
	j2  = SHORT_GET_LOW_BYTE(h);
	
	j  = j1*256+j2;

	h10		= (j/100%10); 		// bai位
	h100	= (j/1000%10);		// qian位	
	h1000	= (j/10000%10);		// wan位
	DelayXms(2);
//	h_10	= _temp_lcd(h10);
//	h_100	= _temp_lcd(h100);
//	h_1000	= _temp_lcd(h1000);

//	hp[0] = SHORT_GET_HIGH_BYTE(h_10);
//	hp[1] = SHORT_GET_LOW_BYTE(h_10);
//	hp[2] = SHORT_GET_HIGH_BYTE(h_100);
//	hp[3] = SHORT_GET_LOW_BYTE(h_100);
//	hp[4] = SHORT_GET_HIGH_BYTE(h_1000);
//	hp[5] = SHORT_GET_LOW_BYTE(h_1000);
	if(t1000 == 1)
	{
		if(t100 <= 8 )
		{
			if(h1000 == 4 || h1000 == 5)
			{
				Write_Lcd_Storage(face_state,1);
			}
			else if(h1000 == 6)
			{
				if(h100 < 5)
				{
					Write_Lcd_Storage(face_state,1);
				}
				else
					Write_Lcd_Storage(face_state,0);
			}
			else
				Write_Lcd_Storage(face_state,0);
		}
		else
			Write_Lcd_Storage(face_state,0);
	}
	else if(t1000 == 2)
	{
		if(t100 <= 8)
		{
			if(h1000 == 4 || h1000 == 5)
			{
				Write_Lcd_Storage(face_state,1);
			}
			else if(h1000 == 6)
			{
				if(h100 < 5)
				{
					Write_Lcd_Storage(face_state,1);
				}
				else
					Write_Lcd_Storage(face_state,0);
			}
			else
				Write_Lcd_Storage(face_state,0);
		}
		else
			Write_Lcd_Storage(face_state,0);
	}
	else
		Write_Lcd_Storage(face_state,0);
	
#if 0
	char a1[2],a2[2],a3[2];
	a1[0] = h10;
	a2[0] = h100;
	a3[0] = h1000;
	DelayXms(200);
	Uart_Tx_buf(a1, 1);		// 打印每一位值
	DelayXms(200);
	Uart_Tx_buf(a2, 1);
	DelayXms(200);
	Uart_Tx_buf(a3, 1);
#endif
	//Lcd_Set_temp(t_1000, t_100, t_10);
	Write_Lcd_Storage(temp_1,t10);
	DelayXms(2);
	Write_Lcd_Storage(temp_10,t100);
	DelayXms(2);
	Write_Lcd_Storage(temp_100,t1000);
	DelayXms(2);
	Write_Lcd_Storage(hum_10,h100);
	DelayXms(2);
	Write_Lcd_Storage(hum_100,h1000);
	Seg_Set_Data();
}
#endif
static void _LCD_Refresh(signed short t, signed short h)
{
    // 提取温度和湿度的位值
    
	long  j,j1,j2;
    unsigned char t10 = t / 10 % 10, t100 = t / 100 % 10, t1000 = t / 1000;
    unsigned char h10, h100, h1000;
	j1	= SHORT_GET_HIGH_BYTE(h);
	j2	= SHORT_GET_LOW_BYTE(h);
		
	j  = j1*256+j2;
	
	h10 	= (h/100%10);		// bai位
	h100	= (h/1000%10);		// qian位	

   
		// 设置面部状态
	   if (t >= 1800 && t <= 2600)
	   {
		   if (h >= 4000 && h <= 7000 )
			   Write_Lcd_Storage(face_state, 1);
		   else
		   Write_Lcd_Storage(face_state, 0);
	   } 
	   else 
	   {
		   Write_Lcd_Storage(face_state, 0);
	   }


		 // 显示温度和湿度
   	 	Write_Lcd_Storage(temp_1, t10);
   	 	Write_Lcd_Storage(temp_10, t100);
    	Write_Lcd_Storage(temp_100, t1000);		
		Write_Lcd_Storage(hum_10, h10);
	   	Write_Lcd_Storage(hum_100, h100);
	
    // 旧代码注释已移除，如果需要调试或输出，可以重新启用
#if 0

    // Debugging or output code
    char a1[2], a2[2], a3[2];
    a1[0] = h10;
    a2[0] = h100;
    a3[0] = h1000;
    DelayXms(200);
    Uart_Tx_buf(a1, 1);
    DelayXms(200);
    Uart_Tx_buf(a2, 1);
    DelayXms(200);
    Uart_Tx_buf(a3, 1);
#endif

    Seg_Set_Data();
}
void Hly_button_Event(void)
{
    signed short f, t, h;
	int temp_data, humi_data;
	Hly_Temp_Hum_Updata();
	delayms(10);

	t = app_data.temp;  // 获取温度

	h = app_data.hum;	// 获取湿度

	temp_data = t/10;
	humi_data = h/100;
	Hly_tuya_updata(humidity_value, humi_data, 4, 2);
	delayms(2);

	Hly_tuya_updata(temp_current, temp_data, 4, 2);
	
//	Hly_Packet_device_manage(report_specified_type, 6, t, 0);		// zigbee上报温度
//	delayms(2);
//	Hly_Packet_device_manage(report_specified_type, 6, h, 1);		// zigbee上报湿度

	if(unit_flag == 0)
	{
    	// 处理温湿度的每一位值
    
		Write_Lcd_Storage(temp_unit, 0);
 	   	_LCD_Refresh(t, h);
	
	}
	if(unit_flag == 1)
	{
	
		Write_Lcd_Storage(temp_unit, 1);
		f = 9*t/5+3200;	
		_LCD_Refresh(f, h);
	}
    
}


void Hly_Temp_Hum_Updata(void)
{

	signed short t, h, f;
    unsigned char Data_f;

  	Data_f = SHT4XReadTemperatureAndHumidity(&t, &h); // 获取温湿度	
  	
	if (Data_f == 1) 
	{

		if(t >= -2000 && t <= 6000)
		{
			app_data.temp = t;
		}
		else
		{
			app_data.temp = 2570;

		}
		if(h >= 0 && t <= 10000)
		{
			app_data.hum = h;
		}
		else
		{
			app_data.hum = 5570;

		}

		if(abs(app_data.last_hum - app_data.hum) > 30 || abs(app_data.last_temp - app_data.temp) > 30)
		{
			app_data.last_hum = app_data.hum;
			app_data.last_temp = app_data.temp;
			if(unit_flag == 0)
			{
		    	// 处理温湿度的每一位值		    
				Write_Lcd_Storage(temp_unit, 0);
		 	   	_LCD_Refresh(app_data.temp, app_data.hum);
			
			}
			if(unit_flag == 1)
			{
			
				Write_Lcd_Storage(temp_unit, 1);
				f = 9*app_data.temp/5+3200;	
				_LCD_Refresh(f, app_data.hum);
			}

		}
		else
		{
			app_data.last_hum = app_data.hum;
			app_data.last_temp = app_data.temp;
		}
	}
}

void App_ZigbeeData_Receive_handle(unsigned char *f_data, char lenthg)
{
	if(f_data[11] == temp_unit_convert)	// 温度单位处理
	{
		unit_flag = f_data[15];
	}
	if(f_data[11] == maxtemp_set)	// 设置温度上限
	{
		app_data.upper_limit_temp = (f_data[15]*256*256*256)+(f_data[16]*256*256)+(f_data[17]*256)+(f_data[18]);
	}
	if(f_data[11] == minitemp_set)	// 设置温度下限
	{	
		app_data.lower_limit_temp = (f_data[15]*256*256*256)+(f_data[16]*256*256)+(f_data[17]*256)+(f_data[18]);
	}
	if(f_data[11] == maxhum_set)	// 设置湿度上限
	{	
		app_data.upper_limit_hum = (f_data[15]*256*256*256)+(f_data[16]*256*256)+(f_data[17]*256)+(f_data[18]);
	}
	if(f_data[11] == minihum_set)	// 设置湿度下限
	{	
		app_data.lower_limit_hum = (f_data[15]*256*256*256)+(f_data[16]*256*256)+(f_data[17]*256)+(f_data[18]);
	}
	if(f_data[11] == temp_periodic_report)	// 设置温度上报间隔
	{
		
	}
	if(f_data[11] == hum_periodic_report)	// 设置湿度上报间隔
	{
		
	}
	if(f_data[11] == temp_sensitivity)	// 设置温度灵敏度
	{
		app_data.temp_sensitivity = f_data[18];
		_data_tx_pack();
		
	}
	if(f_data[11] == hum_sensitivity)	// 设置湿度灵敏度
	{
		app_data.humi_sensitivity = f_data[18];
	}
}

void App_Run(void)
{
	//hly_time_start();
	Zigbee_Cmd_handle();
	
	Zigbee_Config_Set();

	if(hly_get_elapsed_s(app_data.last_up_time) >= 10)
	{
		app_data.last_up_time = get_current_s();
		Hly_Temp_Hum_Updata();
	//	SYS_EnterStop();

	}

}

void app_init(void)
{
	Hly_Packet_Network_msg(query_current_network_status, 0, 0);
	delayms(10);
//	Hly_Temp_Hum_Obtain();
}

